package com.tiniplanet.webgate.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.ui.ModelMap;

import com.tiniplanet.webgate.common.AppConstants;
import com.tiniplanet.webgate.service.EmployeeService;
import com.tiniplanet.webgate.web.model.BaseModel;

public abstract class BaseController {
	
	@Autowired
	private EmployeeService employeeService;

	protected void buildModel(ModelMap modelMap) {
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		String employeeId = auth.getName();
		
		if (!employeeId.equals(AppConstants.ANONYMOUS_USER)) {
			BaseModel model = new BaseModel();
			model.setLoginUser(employeeService.getById(employeeId));
			modelMap.put("model", model);
		}
	}

	// TODO data should be BaseModel
	protected void setModel(ModelMap modelMap, Object data) {
		buildModel(modelMap);
		
		if (data != null) {
			modelMap.put("data", data);
		}
	}
}
